<template>
    <view style="padding-bottom: 120rpx;">
		<j-cell-group>
		<j-cell title="设备序列号" :more="false" @jftap="jftapCopy">
			<view slot="tips" style="padding-right:20rpx;">
				<text style="color: #999999;font-size: 24rpx;">{{info.SerialNo}}</text>
				<uni-icons custom-prefix="iconfont" type="icon-xinxi-" size="24"></uni-icons>
			</view>
		</j-cell>
		<j-cell title="设备登录名" :tips="deviceName" :more="false"></j-cell>
		<j-cell v-if="pid === constant.DEVICE_TYPE.EE_DEV_NORMAL_MONITOR" title="ICCID" :tips="iccid" :more="false"></j-cell>
		<j-cell title="设备硬件" :tips="info.HardWare" :more="false"></j-cell>
		<j-cell title="硬件版本号" :tips="info.HardWareVersion" :more="false"></j-cell>
		<j-cell title="软件版本号" :tips="info.SoftWareVersion" :more="false"></j-cell>
		<j-cell title="加密版本号" :tips="info.EncryptVersion" :more="false"></j-cell>
		<j-cell title="发布日期" :tips="info.BuildTime" :more="false"></j-cell>
		<j-cell v-if="(pid === constant.DEVICE_TYPE.EE_DEV_BOUTIQUEROBOT || pid === constant.DEVICE_TYPE.EE_DEV_NORMAL_MONITOR)&& release" title="发布状态" :tips="release" :more="false"></j-cell>
		<j-cell v-if="pid === constant.DEVICE_TYPE.EE_DEV_BOUTIQUEROBOT || pid === constant.DEVICE_TYPE.EE_DEV_NORMAL_MONITOR" title="制造方式" :tips="manufacturing" :more="false"></j-cell>
		<j-cell v-if="pid === constant.DEVICE_TYPE.EE_DEV_BOUTIQUEROBOT || pid === constant.DEVICE_TYPE.EE_DEV_NORMAL_MONITOR" title="产品归属码" :tips="PCMac" :more="false"></j-cell>
		<j-cell v-if="pid === constant.DEVICE_TYPE.EE_DEV_BOUTIQUEROBOT || pid === constant.DEVICE_TYPE.EE_DEV_NORMAL_MONITOR" title="设备时区" :tips="timeMin" :more="false"></j-cell>
		<j-cell title="设备时间" :tips="deviceTime" :more="false"></j-cell>
		<!-- <j-cell title="修改日期" :tips="info.UpdataTime" :more="false"></j-cell>
		<j-cell title="修改内容" :tips="updataTypeText" :more="false"></j-cell> -->
		</j-cell-group>
		<view class="bottom-fixed">
			<view class="u-f-ajc" style="display: flex; flex-direction: column;width: 100%;">
			    <view class="btn"  @tap="bindtapReset">恢复出厂设置</view>
			    <view class="btn"  @tap="bindtapRestart">设备重启</view>
			</view>
		</view>
    </view>
</template>

<script>
const  { date2FmtStr } = require('@/utils/util.js');
// const constant = getApp().globalData.constant;
export default {
    data() {
        return {
            token: '',
			deviceNo:'',
			deviceName:'',
			pid:null,
			isSupportPTZDevice:false,
            info: {
                SerialNo: '',
                HardWare: '',
                HardWareVersion: '',
                SoftWareVersion: '',
                EncryptVersion: '',
                BuildTime: '',
                UpdataTime: '',
            },
			timeMin: '', //设备时区
			PCMac: '', //产品归属码
			manufacturing: '', //制造方式
			release: '',//发布状态
			deviceTime:'',
            updataTypeText: '',
			iccid:'',
			constant:getApp().globalData.constant
        };
    }
    /**
     * 生命周期函数--监听页面加载
     */,
    onLoad: function (options) {
		console.log(options)
        this.token = options.token
		this.deviceName = options.deviceName
		this.deviceNo = options.deviceNo
		this.pid = options.pid
		console.log(this.pid,'显示')
		this.isSupportPTZDevice = JSON.parse(options.isSupportPTZDevice) //是否是摇头机
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {},
    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {
		this.getinfo();
		this.getDeviceTime();
		this.reqCaps()
		if(this.pid === this.constant.DEVICE_TYPE.EE_DEV_BOUTIQUEROBOT || this.pid === this.constant.DEVICE_TYPE.EE_DEV_NORMAL_MONITOR){
			this.FishLensParam() //产品归属地
			this.getTimeZone()//设备时区
			this.queryDevice()//制造方式
		}
	},
    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {},
    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {},
    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {},
    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {},
    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {},
    methods: {
		reqCaps(v) {
			uni.showLoading();
			let token = this.token;
			let data = {
				"sn":"4fa9181dfe308d08",
				"caps":[
					"net.cellular.iccid",
				]
			};
			getApp().globalData.api.reqCaps(data, token, (result) => {
				console.log(result,'数据数据')
				this.iccid = result.caps['net.cellular.iccid']
				uni.hideLoading()
			});
		},
        // 第4步-3-1 获取设备基础信息
        getinfo() {
            uni.showLoading({
                title: '获取设备基础信息...'
            });
            let token = this.token;
            const data = {
                Name: 'SystemInfo'
            };
            getApp().globalData.api.getinfo(data, token, (result) => {
                console.log('获取设备基础信息:', result);
                uni.hideLoading()
				this.info = result.SystemInfo
				this.findRelease()//发布状态
				let updataType = getApp().globalData.constant.UPDATA_TYPE;
				Object.keys(updataType).forEach((item) => {
					if (item === this.info.UpdataType) {
						this.updataTypeText = updataType[item]
					}
				});
            });
			
        },
		queryDevice() {
			let token = this.token;
			const data = {
				sn: this.deviceNo
			};
			getApp().globalData.api.queryDevice(data, token, (result) => {
				// console.log('制造方式:', result);
				if (result.device.chipOemId <= 8) {
					this.manufacturing = 'original(' + result.device.chipOemId + ')'
				} else {
					this.manufacturing = 'licensee(' + result.device.chipOemId + ')'
				}
		
			});
		},
		// 发布状态
		findRelease() {
			const data = {
				devId: this.deviceNo,
				releaseDate: this.info.BuildTime
			};
			getApp().globalData.api.getfindRelease(data, (result) => {
				console.log('发布状态:',result);
				this.release = result.release?'Release':'Debug'
				this.$forceUpdate()  
			});
		},
		// 产地归属码
		FishLensParam() {
			this.getconfig('Camera.FishLensParam')
		},
		// 获取时区
		getTimeZone() {
			this.getconfig('System.TimeZone')
		},
		getconfig(v) {
			uni.showLoading();
			let token = this.token;
			let data = {
				Name: v,
			};
			getApp().globalData.api.getconfig(data, token, (result) => {
				uni.hideLoading()
				if (v == 'System.TimeZone') {
					this.timeMin = '东' + result['System.TimeZone'].timeMin / -60
				} else if (v == 'Camera.FishLensParam') {
					this.PCMac = result['Camera.FishLensParam'][0].PCMac
				}
			});
		},
		//获取设备时间
		getDeviceTime(){
			let token = this.token;
			const data = {  Name: 'OPTimeQuery' };
			getApp().globalData.api.opdev(data, token, (result) => {
			    console.log('获取设备时间:', result);
			    this.deviceTime = result.OPTimeQuery
				let date = new Date(result.OPTimeQuery)
				let time = date.getTime()
				setInterval(()=>{
					time = time + 1000
					this.deviceTime = date2FmtStr(new Date(time))
				},1000)
			});
		},
		
		jftapCopy(){
			uni.setClipboardData({
				data:this.info.SerialNo,
				success: function () {
					console.log('success');
				}
			})
		},

        // 恢复出厂设置
        bindtapReset() {
            uni.showModal({
                title: '提示',
                content: '确认恢复出厂设置？',
                success: (res) => {
                    if (res.confirm) {
                        uni.showLoading({
                            title: '恢复中...'
                        });
                        let token = this.token;
                        let data = {
                            Name: 'OPDefaultConfig',
                            OPDefaultConfig: {
                                Account: true,
                                Alarm: true,
                                CommPtz: true,
                                Encode: true,
                                General: true,
                                NetCommon: true,
                                NetServer: true,
                                Preview: true,
                                Record: true,
                                XMModeSwitch: true,
                                CameraPARAM: true
                            }
                        };
                        getApp().globalData.api.opdev(data, token, (result) => {
                            uni.hideLoading({
                                success: (res) => {
                                    console.log(result);
									getApp().globalData.mqtt.getUnsubscribe(this.deviceNo,'device')
									uni.navigateBack({
										delta:4
									})
                                }
                            });
                        });
                    }
                }
            });
        },

        // 重启
        bindtapRestart() {
            uni.showModal({
                title: '提示',
                content: '确认重启设备？',
                success: (res) => {
                    if (res.confirm) {
                        uni.showLoading({
                            title: '重启中...'
                        });
                        let token = this.token;
                        let data = {
                            Name: 'OPMachine',
                            OPMachine: {
                                Action: 'Reboot'
                            }
                        };
                        getApp().globalData.api.opdev(data, token, (result) => {
                            uni.hideLoading({
                                success: (res) => {
                                    console.log(result);
									uni.navigateBack({
										delta:4
									})
                                }
                            });
                        });
                    }
                }
            });
        }
    }
};
</script>
<style>
	.btn{
		border-radius: 10rpx;
		height: 90rpx;
		background: white;
		width: 90%;
		margin-top: 30rpx;
		color: #FF0025;
		display: flex;
		justify-content: center;
		align-items: center;
	}
	
</style>
